package com.hs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hs.entity.TbMenu;
import com.hs.service.MenuService;
import com.hs.service.RoleService;
import com.hs.utils.ResultUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

/*
 * @Author:iWeeper
 * @Date:2022/6/21 12:38
 * @Version:1.0
 * */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @Autowired
    private MenuService menuService;

    @RequestMapping("/roleList")
    private String roleList(){
        return "manager/role-list";
    }

    @RequestMapping("/roleList/data")
    protected void roleListJson(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        //查询角色列表
        List<Map<String,Object>> list = roleService.queryAll();
        ResultUtils.success(response,list,"ok");
    }

    @RequestMapping("/menuTree")
    protected String menuTree(Model model, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try{
            //获取角色ID
            String roleId = request.getParameter("roleId");
            //查询授权菜单的ID
            String menuIds = menuService.queryMenuIdsByRoleId(Integer.parseInt(roleId));
            //查询菜单列表
            List<Map<String,Object>> list = menuService.queryAll();
            model.addAttribute("menuTree", JSONObject.toJSONString(list));
            model.addAttribute("menuIds",menuIds);
            model.addAttribute("roleId",roleId);
        }catch (SQLException e){
            e.printStackTrace();
        }
        return "manager/role-menu-tree";
    }

    @RequestMapping("/deleteMenu")
    private void deleteMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id= request.getParameter("id");
        try {
            Integer rows=menuService.deleteById(Integer.valueOf(id));
            if(rows==1){
                ResultUtils.success(response,"删除成功");
            }else {
                ResultUtils.error(response,20,"删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            ResultUtils.error(response,21,"数据库异常，请联系管理员");
        }
    }
    private void deleteMenuBatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String ids= request.getParameter("ids");
        try {
            Integer rows=menuService.deleteBatch(ids);
            if(rows>0){
                ResultUtils.success(response,"删除成功");
            }else {
                ResultUtils.error(response,20,"删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            ResultUtils.error(response,21,"数据库异常，请联系管理员");
        }
    }
    //新增菜单
    private void menuAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/WEB-INF/page/manager/menu-add.jsp").forward(request,response);
    }
    private void createMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name=request.getParameter("name");
        try {
            Integer row=menuService.createMenu(name);
            if (row==1){
                ResultUtils.success(response,"创建成功");
            }else {
                ResultUtils.error(response,321,"创建失败");
            }
        } catch (SQLException throwables) {
            ResultUtils.error(response,320,"系统异常，请联系管理员");
        }
    }
    //编辑菜单
    private void menuEdit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        try {
            TbMenu menu=menuService.queryById(Integer.valueOf(id));
            request.setAttribute("userInfo",menu);
            request.getRequestDispatcher("/WEB-INF/page/manager/menu-edit.jsp").forward(request,response);
        } catch (SQLException throwables) {
            throwables.printStackTrace();

        }
    }

    private void updateMenu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String,String[]> params=request.getParameterMap();
        try {
            Integer rows=menuService.updateMenu(params);
            if (rows==1){
                ResultUtils.success(response,"修改成功");
            }else {
                ResultUtils.error(response,2,"修改失败");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            ResultUtils.error(response,1,"系统异常，请联系管理员");
        }
    }
}
